package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 宋
 * @since 2022-06-06
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * //Request URL: http://127.0.0.1:8080/category/page?page=1&pageSize=10
     * //Request Method: GET
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize) {
        log.info("page= {},pageSize={} ", page, pageSize);
        // 1 . 分页构造器
        Page page1 = new Page(page, pageSize);
        // 2 . 查询构造器 + 排序规则
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);  //按照sort自动排序 规则Asc
        Page pageR = categoryService.page(page1, queryWrapper);
        return R.success(pageR);

    }

    /**
     * 添加菜品
     * Request URL: http://127.0.0.1:8080/category
     * Request Method: POST
     * {name: "111", type: "1", sort: "111"}
     */
    @PostMapping
    public R<String> add(@RequestBody Category category) {
        log.info("收到参数-->{}", category.getName() != null);
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 根据id删除菜
     * Request URL: http://127.0.0.1:8080/category?id=1397844263642378242
     * Request Method: DELETE
     * id: 1397844263642378242
     */
    @DeleteMapping
    public R<String> delete(@Param("id") Long id) {

        //要判断菜品是否被其他表关联了 如果关联就不能删除  一对多
        log.info("删除分类id={}", id);
        //categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("当前菜品分类删除成功");
    }

    /**
     * 修改
     * Request URL: http://127.0.0.1:8080/category
     * Request Method: PUT
     * {id: "1397844263642378242", name: "湘菜", sort: "12"}
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        log.info("修改对象的id={}", category.getId());
        //参数前端传来的 只需要执行业务
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 添加菜品-->菜品分类查询
     * Request URL: http://127.0.0.1:8080/category/list?type=1
     * Request Method: GET
     * type: 1
     */
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        //查询条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(type!=null,Category::getType,type);
        //排序条件
        queryWrapper.orderByAsc(Category::getCreateTime).orderByAsc(Category::getSort);
        //查询
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }
}

